package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * ClassName: DishServiceImpl
 * Description: com.sky.service.impl
 */
@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private SetMealDishMapper setMealDishMapper;

    /**
     * 菜品分页查询
     * @param dishPageQueryDTO
     * @return
     */
    public PageResult dishPage(DishPageQueryDTO dishPageQueryDTO) {

        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());

        Page<DishVO> page = dishMapper.dishPage(dishPageQueryDTO);

        PageResult pageResult = new PageResult();
        pageResult.setTotal(page.getTotal());
        pageResult.setRecords(page.getResult());

        return pageResult;
    }

    /**
     * 添加菜品和对应的口味
     * @param dishDTO
     */
    @Transactional
    public void addDishWithFlavor(DishDTO dishDTO) {

        Dish dish = new Dish();

        BeanUtils.copyProperties(dishDTO, dish);

        //默认状态启用
//        dish.setStatus(StatusConstant.ENABLE);

        dishMapper.insert(dish);

        //获取insert语句生成的主键值
        Long dishId = dish.getId();

        //获取口味
        List<DishFlavor> flavors = dishDTO.getFlavors();

        if(flavors != null && flavors.size() > 0) {
            for(DishFlavor df : flavors) {
                df.setDishId(dishId);
            }

            // 向口味表插入n条数据
            dishFlavorMapper.insertBatch(flavors);
        }
    }

    /**
     * 批量删除菜品
     * @param ids
     */
    @Transactional
    public void deleteDish(List<Long> ids) {

        // 启售中的菜品不能删除
        for(Long id : ids) {

            if(dishMapper.getDishStatusById(id) == StatusConstant.ENABLE) {

                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }

        // 关联了套餐的菜品不能删除
        List<Long> setMealIds = setMealDishMapper.getSetMealIdsByDishIds(ids);
        if(setMealIds != null && setMealIds.size() > 0) {

            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        // 删除菜品和对应的菜品口味
        for(Long id : ids) {
            dishMapper.deleteById(id);

            dishFlavorMapper.deleteByDishId(id);
        }
    }

    /**
     * 菜品起售、停售
     * @param status
     * @param id
     */
    public void dishStatus(Integer status, Long id) {

        dishMapper.dishStatus(status, id);

    }

    /**
     * 修改菜品
     * @param dishDTO
     */
    @Transactional
    public void updateDish(DishDTO dishDTO) {

        Dish dish = new Dish();

        BeanUtils.copyProperties(dishDTO, dish);

        dishMapper.update(dish);

        // 删除口味表中对应的菜品口味数据
        dishFlavorMapper.deleteByDishId(dish.getId());

        // 插入你要修改的菜品口味数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors != null && flavors.size() > 0) {
            for(DishFlavor df : flavors) {
                df.setDishId(dish.getId());
            }

            dishFlavorMapper.insertBatch(flavors);

        }

    }

    /**
     * 根据id查询菜品
     * @param id
     * @return
     */
    @Transactional
    public DishVO getDishById(Long id) {

        // 1. 查询菜品基本信息
        DishVO dishVO = dishMapper.getDishById(id);

        // 2. 查询口味列表
        List<DishFlavor> flavors = dishFlavorMapper.getFlavorsByDishId(id);

        // 3. 组装数据
        dishVO.setFlavors(flavors);

        return dishVO;
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    public List<Dish> getDishByCategoryId(Long categoryId) {

        List<Dish> dishes = dishMapper.getDishByCategoryId(categoryId);

        return dishes;
    }

    /**
     * 查询菜品和对应口味
     * @param dish
     * @return
     */
    public List<DishVO> getDishWithFlavor(Dish dish) {
        Long categoryId = dish.getCategoryId();
        List<Dish> dishes = dishMapper.getDishByCategoryId(categoryId);

        List<DishVO> dishVOList = new ArrayList<>();

        for(Dish d : dishes) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d, dishVO);
            List<DishFlavor> flavors = dishFlavorMapper.getFlavorsByDishId(d.getId());
            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);

        }

        return dishVOList;
    }
}
